#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>

void display(const char*);

int main(int argc,char** argv)
{
  display(argv[1]);

  return 0;
}

void display(const char* path)
{
  DIR* dir;
  dir = opendir(path);
  if(dir == NULL)
  {
    fprintf(stderr,"%s is open fail\n",path);
    return;
  }

  struct dirent* di;
  struct stat sta;
  int ret;
  char filepath[1024];

  while((di = readdir(dir)))
  {
    snprintf(filepath,1024,"%s/%s",path,di->d_name);  
    
    ret = stat(filepath,&sta);
    if(ret < 0)
    {
      fprintf(stderr,"%s is stat fail\n",filepath);
      return ;
    }

    if(S_ISDIR(sta.st_mode))
    {
      if(!strcmp(di->d_name,".") || !strcmp(di->d_name,".."))
      {
        continue;
      }
      printf("dir is %s\n",di->d_name);
      display(filepath);
    }

    if(S_ISREG(sta.st_mode))
    {
      printf("regular file is %s ",di->d_name);
    }
    printf("\n");
  }
}
